package com.jerry.template.module.mainpage.shouye.duanzi.activity;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.shouye.duanzi.adapter.ActivityTagJokeListContentAdapter;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.TAGDetail;
import com.jerry.template.utils.AsyncTask;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class TAGDuanziActivity extends BaseActivity {

    @BindView(R.id.tag_duanzi_listView)
    PullToRefreshListView listView;

    @BindView(R.id.tag_duanzi_back)
    ImageButton btn_back;

    private ActivityTagJokeListContentAdapter mAdapter = null;
    private List<TAGDetail.DataBean.DataBean1> mDataBean = new ArrayList<>();
    private int index = 1;

    @Override
    protected void initViews() {
        mAdapter = new ActivityTagJokeListContentAdapter(this,mDataBean);
        listView.setAdapter(mAdapter);
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                index = 1;
                mDataBean.clear();
                loadData();
                mAdapter.notifyDataSetChanged();
                new FinishRefresh().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                index++;
                loadData();
                mAdapter.notifyDataSetChanged();
                new FinishRefresh().execute();
            }
        });

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class FinishRefresh extends AsyncTask{

        @Override
        protected Object doInBackground(Object[] params) {
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            listView.onRefreshComplete();
        }
    }

    @Override
    protected void loadData() {
        Map<String,Object> params = new HashMap<>();
        params.put("essence",index);
        OkHttpUtils.postSubmitForm(Urls.DUANZI_TAG,null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        super.postEventResult(message);
        if (message != null) {
            if (message.type.equals(Urls.DUANZI_TAG)) {
                String result = message.result;
                Gson gson = new Gson();
                TAGDetail tagDetail = gson.fromJson(result,TAGDetail.class);
                mDataBean.addAll(tagDetail.getData().getData());
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tagduanzi;
    }
}
